/*
 * JHeuristics - The Java Meta-Heuritics Library
 * Copyright (C) 2004-2005, Universidade Federal de Campina Grande. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.jheuristics.ga.operators.selectors.roulettes;

import java.io.Serializable;

import org.epanetgrid.otimizacao.log.AGLogger;
import org.jheuristics.util.RandomGenerator;


/**
 * TODO
 *
 * @author Marcell Manfrin, marcell@ourgrid.org, Jan 9, 2006
 */
public class SUSRoulette extends AbstractRoulette implements Serializable {

	/**
	 * TODO
	 *
	 * @param probabilities
	 * @param deltaSpin
	 */
	public SUSRoulette(double[] values, RandomGenerator random) {
		super(values, random);
	}

	/**
	 * TODO
	 *
	 * @param values
	 * @param minimalValue
	 * @param deltaSpin
	 */
	public SUSRoulette(double[] values, double minimalValue, RandomGenerator random) {
		super(values, minimalValue, random);
	}

	/**
	 * TODO
	 *
	 * @param howMany
	 * @return
	 * @see org.jheuristics.ga.operators.selectors.Roulette#spin(int)
	 */
	public int[] spin(int howMany) {
		super.spin(howMany);
		int result[] = new int[howMany];
		if (howMany > 0) {
			double[] probabilities = getProbabilities();
			double delta = 1. / howMany;
			double spin = getRandom().nextDouble();
	
			for (int i = 0; i < howMany; i++) {
				StringBuffer buffer = new StringBuffer("N�mero gerado: ");
				buffer.append(spin);
				AGLogger.getInstance().debug(buffer);
		        for (int j = 0; j < probabilities.length; j++) {
		            if (probabilities[j] > spin) {
						result[i] = j;
						break;
		            }
		        }
				spin += delta;
		        if (spin > 1.0) {
					spin -= 1.0;
		        }
			}
		}
		return result;
	}

}
